const errorTypes = require("../constants/error-types");

const errorHandler = (error, ctx) => {
	let status, message;
	switch (error.message) {
		case errorTypes.USER_ALREADY_EXISTS:
			status = 409; // Conflict
			message = "用户名已存在";
			break;
		case errorTypes.USER_DOES_NOT_EXISTS:
			status = 400; //Bad Request
			message = "用户名不存在";
			break;
		case errorTypes.PASSWORD_IS_INCORRENT:
			status = 400; //Bad Request
			message = "密码错误";
			break;
		case errorTypes.UNAUTHORIZATION:
			status = 401; //unauthorization
			message = "无效token";
			break;
		case errorTypes.UNPERMISSION:
			status = 401; //unpermission
			message = "没有权限";
			break;
		default:
			status = 404;
			message = "NOT FOUND";
	}
	ctx.status = status;
	ctx.body = {
		code: status,
		message
	};
};

module.exports = errorHandler;
